package com.tyk.bookstore.back.product.controller;


import cn.dev33.satoken.annotation.SaCheckPermission;
import com.tyk.bookstore.back.common.core.model.result.R;
import com.tyk.bookstore.back.product.model.query.ClazzAddQuery;
import com.tyk.bookstore.back.product.model.query.ClazzListQuery;
import com.tyk.bookstore.back.product.model.query.ClazzPageQuery;
import com.tyk.bookstore.back.product.model.query.ClazzUpdateQuery;
import com.tyk.bookstore.back.product.model.vo.ClazzListVo;
import com.tyk.bookstore.back.product.model.vo.ClazzNestVo;
import com.tyk.bookstore.back.product.model.vo.ClazzPageVo;
import com.tyk.bookstore.back.product.model.vo.ClazzVo;
import com.tyk.bookstore.back.product.service.ClazzService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * @author tyk
 * @description 分类
 */
@Tag(name = "商品管理/分类管理", description = "分类管理")
@RequiredArgsConstructor
@RestController
@RequestMapping("api/product/clazz")
public class ClazzController {

    private final ClazzService clazzService;

    @Operation(summary = "查询分类详情", description = "查询分类详情")
    @SaCheckPermission("product:clazz:get")
    @GetMapping("{id}")
    public R<ClazzVo> getById(
            @Schema(description = "分类ID")
            @NotNull @Min(value = 1)
            @PathVariable Long id
    ) {
        return R.ok(clazzService.getById(id));
    }


    @Operation(summary = "查询嵌套分类列表", description = "查询嵌套分类列表")
    @SaCheckPermission("product:clazz:list:nest")
    @GetMapping({"list/nest"})
    public R<List<ClazzNestVo>> getNestList() {
        return R.ok(clazzService.getNestList());
    }


    @Operation(summary = "查询分类列表", description = "查询分类列表")
    @SaCheckPermission("product:clazz:list")
    @GetMapping({"list"})
    public R<List<ClazzListVo>> getList(
            @Schema(description = "分类列表查询参数")
            @ParameterObject
            @Validated ClazzListQuery query
    ) {
        return R.ok(clazzService.getList(query));
    }

    @Operation(summary = "分页查询分类", description = "分页查询分类")
    @SaCheckPermission("product:clazz:page")
    @GetMapping("page")
    public R<ClazzPageVo> getPage(
            @Schema(description = "分类分页查询参数")
            @ParameterObject
            @Validated ClazzPageQuery query
    ) {
        return R.ok(clazzService.getPage(query));
    }

    @Operation(summary = "添加分类", description = "添加分类")
    @SaCheckPermission("product:clazz:add")
    @PostMapping
    public R<Object> add(
            @Validated @RequestBody ClazzAddQuery query
    ) {
        clazzService.add(query);
        return R.ok();
    }

    @Operation(summary = "更新分类", description = "更新分类")
    @SaCheckPermission("product:clazz:update")
    @PutMapping
    public R<Object> updateById(
            @Validated @RequestBody ClazzUpdateQuery query
    ) {
        clazzService.updateById(query);
        return R.ok();
    }

    @Operation(summary = "删除分类", description = "删除分类")
    @SaCheckPermission("product:clazz:delete")
    @DeleteMapping("{id}")
    public R<Object> deleteById(
            @Schema(description = "分类ID")
            @Min(value = 1) @NotNull
            @PathVariable Long id
    ) {
        clazzService.deleteById(id);
        return R.ok();
    }

    @Operation(summary = "批量删除分类", description = "批量删除分类")
    @SaCheckPermission("product:clazz:delete")
    @DeleteMapping
    public R<Object> deleteBatchByIdSet(
            @Schema(description = "分类ID列表")
            @NotEmpty @NotNull Set<Long> idList
    ) {
        clazzService.deleteBatchByIdSet(idList);
        return R.ok();
    }
}

